﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Basic_dictsubject : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridView(true);
            BindDDL();
        }
    }

    //分页触发事件
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindGridView(false);
    }

    protected void BindGridView(bool GetCount)
    {
        PM.Code.Web.controlbind.BindGridViewPublicProc(GridView1, AspNetPager1, "Dict_Subject", "id" , "id,subject1,subject2,isuse", "id asc", "1=1", GetCount);
    }

    protected void BindDDL()
    {
         //绑定学科门
        PM.Code.Web.controlbind.BindDropDownList(this.ddlSubject, "select distinct(subject1) from Dict_Subject where isuse='True' order by subject1", "subject1", "subject1");
        this.ddlSubject.Items.Insert(0,new ListItem("——学科门——", "0"));
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //实例化字典类
        PM.PMBasic.DictData dict = new PM.PMBasic.DictData();
        int id = Convert.ToInt32(GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
        if (e.CommandName == "delsubject")
        {
            if (dict.Delete_DictSubject(id) > 0)
            {
                PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "已删除此学科一级类！");
            }
        }
        else if (e.CommandName == "open")
        {
            if (dict.Update_DictSubjectStatus(id,"True") > 0)
            {
                PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "已启用此学科一级类！");
            }
            BindGridView(true);
        }
        else if (e.CommandName == "close")
        {
            if (dict.Update_DictSubjectStatus(id, "False") > 0)
            {
                PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "已禁用此学科一级类！");
            }
            BindGridView(true);
        }
    }
    //结束编辑
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridView1.EditIndex = -1;
        BindGridView(false);
    }
    //行编辑
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindGridView(false);
    }
    //行更新
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //实例化字典类
        PM.PMBasic.DictData dict = new PM.PMBasic.DictData();
        //获取行主键值
        int id = Convert.ToInt16(GridView1.DataKeys[e.RowIndex].Value);
        //获取修改值
        string subjectname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.Trim();
        if (subjectname == string.Empty)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "数据不能为空");
            return;
        }
        dict.Update_DictSubject(subjectname, id);
        PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "更新成功！");
        this.GridView1.EditIndex = -1;
        BindGridView(false);
    }
    //新增加一个学科一级类
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //实例化字典类
        PM.PMBasic.DictData dict = new PM.PMBasic.DictData();
        if (this.ddlSubject.SelectedIndex == 0)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "您未选择学科门！");
            return;
        }
        if (this.txtField2.Text.Trim() == string.Empty)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "一级类名不能为空！");
            return;
        }
        string subject1=this.ddlSubject.SelectedValue.Trim();
        string subject2=this.txtField2.Text.Trim();
        if(dict.Insert_DictSubject(subject1,subject2)>0)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "一级类添加成功！");
            BindGridView(true);
            this.txtField2.Text = "";
            this.ddlSubject.SelectedIndex = 0;
        }
    }
    //新增加一个学科门
    protected void Button2_Click(object sender, EventArgs e)
    {
        //实例化字典类
        PM.PMBasic.DictData dict = new PM.PMBasic.DictData();
        if (this.txtField1.Text.Trim() == string.Empty)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "学科门不能为空！");
            return;
        }
        if (this.txtField2.Text.Trim() == string.Empty)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "一级类名不能为空！");
            return;
        }
        string subject1 = this.txtField1.Text.Trim();
        string subject2 = this.txtField2.Text.Trim();
        if (dict.Insert_DictSubject(subject1, subject2) > 0)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "学科门添加成功！");
            BindGridView(true);
            this.txtField2.Text = "";
            this.txtField1.Text = "";
        }
        this.ddlSubject.Items.Add(new ListItem(subject1,subject1));
    }
    //删除一个学科门
    protected void Button3_Click(object sender, EventArgs e)
    {
        //实例化字典类
        PM.PMBasic.DictData dict = new PM.PMBasic.DictData();
        if (this.ddlSubject.SelectedIndex==0)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "您未选择学科门！");
            return;
        }
        string subject = this.ddlSubject.SelectedValue.Trim();
        if (dict.Delete_DictSubject1(subject) > 0)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "学科门删除成功！");
            BindGridView(true);
        }
        else
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "学科门删除失败！");
        this.ddlSubject.Items.Remove(subject);
    }
    //修改一个学科门
    protected void Button4_Click(object sender, EventArgs e)
    {
        //实例化字典类
        PM.PMBasic.DictData dict = new PM.PMBasic.DictData();
        if (this.ddlSubject.SelectedIndex == 0)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "您未选择学科门！");
            return;
        }
        if (this.txtField1.Text.Trim()==string.Empty)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "学科门不能为空！");
            return;
        }
        string oldsubject = this.ddlSubject.SelectedValue.Trim();
        string newsubject = this.txtField1.Text.Trim();
        if (dict.Update_DictSubject1(oldsubject,newsubject) > 0)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "学科门修改成功！");
            BindGridView(true);
        }
        else
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "学科门修改失败！");
        this.ddlSubject.Items.Remove(oldsubject);
        this.ddlSubject.Items.Add(new ListItem(newsubject,newsubject));
        this.ddlSubject.SelectedIndex = 0;
        this.txtField1.Text = "";
    }
}
